package com.pr.makfilm.user.pages.school.list.cmp;

import com.pr.makfilm.core.ajax.ConfirmationCallListener;
import com.pr.makfilm.core.cmp.button.BootstrapLink;
import com.pr.makfilm.core.cmp.notify.Notification;
import com.pr.makfilm.core.cmp.table.AjaxDataTable;
import com.pr.makfilm.core.cmp.table.toolbar.FilterToolbar;
import com.pr.makfilm.domain.District;
import com.pr.makfilm.domain.School;
import com.pr.makfilm.domain.filter.OpenSearchDescription;
import com.pr.makfilm.service.IDistrictService;
import com.pr.makfilm.service.ISchoolService;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Pawel
 * Date: 25.09.13
 * Time: 22:31
 * To change this template use File | Settings | File Templates.
 */
public class SchoolFilterToolbar extends FilterToolbar<School>
{
   @SpringBean
   private ISchoolService schoolService;

   @SpringBean
   private IDistrictService districtService;

   private AjaxLink<Void> newSchool;

   private BootstrapLink editSchool;

   private BootstrapLink deleteSchool;

   private SchoolWindow window;

   private List<District> districts;

   public SchoolFilterToolbar(final AjaxDataTable<School> table, IModel<School> model)
   {
      super(table);
      setOutputMarkupId(true);
      setDefaultModel(model);
   }

   @Override
   protected void onConfigure()
   {
      districts = districtService.findUserDistricts();
   }

   @Override
   protected void onInitialize()
   {
      super.onInitialize();
      window = new SchoolWindow("window")
      {

         @Override
         protected void onAfterSubmit(AjaxRequestTarget target)
         {
            refreshTableView(target);
         }
      };
      add(window);
      newSchool = new AjaxLink<Void>("new")
      {
         @Override
         public void onClick(AjaxRequestTarget target)
         {
            if (districts != null && districts.size() > 0)
            {
               window.resetState(true);
               window.setDistricts(districts);
               window.show(target);
            }
            else
            {
               Notification.error(getString("no.districts.found"));
            }
         }
      };
      add(newSchool);
      editSchool = new BootstrapLink("edit")
      {
         @Override
         public void onClick(AjaxRequestTarget target)
         {
            window.resetState(false);
            window.setSchool(getSelected());
            window.setDistricts(districts);
            window.show(target);
         }

         @Override
         protected void onConfigure()
         {
            setEnabled(getSelected() != null);
         }
      };
      add(editSchool);

      deleteSchool = new BootstrapLink("delete")
      {
         @Override
         public void onClick(AjaxRequestTarget target)
         {
            schoolService.delete(getSelected());
//            setDefaultModelObject();
            Notification.success(getString("school.successfully.deleted"));
            refreshTableView(target);
         }

         @Override
         protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
         {
            super.updateAjaxAttributes(attributes);
            attributes.getAjaxCallListeners().add(new ConfirmationCallListener(getString("school.delete.confirm")));
         }

         @Override
         protected void onConfigure()
         {
            setEnabled(getSelected() != null);
         }
      };
      add(deleteSchool);
   }

   @Override
   public void registerFilters(DataTable<?, ?> table, OpenSearchDescription<School> osd)
   {
      addFilter(new TextField<String>("name", new PropertyModel<String>(osd, "filter.name")), getString("name"));
      DropDownChoice<District> dropDownChoice = new DropDownChoice<District>("district", new PropertyModel<District>(osd, "filter.district"), new PropertyModel<List<District>>(this, "districts"),
              new IChoiceRenderer<District>()
              {
                 @Override
                 public Object getDisplayValue(District object)
                 {
                    return object.getName();
                 }

                 @Override
                 public String getIdValue(District object, int index)
                 {
                    return object.getSid();
                 }
              });
      dropDownChoice.setNullValid(true);
      addFilter(dropDownChoice, getString("district"));
   }

   private void refreshTableView(AjaxRequestTarget target)
   {
      target.add(table);
      target.add(editSchool);
      target.add(deleteSchool);
   }

   private School getSelected()
   {
      return (School) getDefaultModelObject();
   }
}
